package com.tjetc.impl;

import com.tjetc.dao.PropertyserviceMapper;
import com.tjetc.domian.Propertyservice;
import com.tjetc.service.ServiceService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class ServiceServiceImpl implements ServiceService {
    private PropertyserviceMapper propertyserviceMapper = Db.getMapper(PropertyserviceMapper.class);
    private Result result = new Result();


    @Override
    public Result add(Propertyservice propertyservice) {
     if (propertyservice!=null){
         if (propertyserviceMapper.findByName(propertyservice.getServicename())!=null){
             return new Result(500,"该物业服务已存在",null);
         }else{
             int i = propertyserviceMapper.insert(propertyservice);
             return i==1?new Result(200,"添加成功",propertyservice):new Result(500,"添加失败",null);
         }
     }else {
         return new Result(500,"添加内容不能为空",null);
     }
 }


    @Override
    public List<Propertyservice> select() {
        return propertyserviceMapper.select();
    }

    @Override
    public Result update(Propertyservice propertyservice) {
        if (propertyservice ==null) {
            return result.setCode(500).setMsg("不能修改为空内容").setData(null);
        }
        if (propertyserviceMapper.findByName(propertyservice.getServicename()).equals(propertyservice)){
            return result.setCode(500).setMsg("不能修改为重复的内容").setData(null);
        }else {
            int i = propertyserviceMapper.updata(propertyservice);
            return result.setCode(200).setMsg("修改成功").setData(propertyservice);
        }

    }

    @Override
    public Result del(int serviceid) {
        if (propertyserviceMapper.findById(serviceid)){
            int i = propertyserviceMapper.deleteByPrimaryKey(serviceid);
            return i==1?result.setCode(200).setMsg("删除成功").setData(null):result.setCode(500).setMsg("删除失败").setData(null);
        }else {
            return result.setCode(500).setMsg("删除内容不存在").setData(null);
        }

    }


}
